
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include <unistd.h>
#include"/home/xuhuinan1/ds18b20/mosquitto-2.0.9/iniparser.h"
#include"/home/xuhuinan1/ds18b20/mosquitto-2.0.9/dictionary.h"
#include"/home/xuhuinan1/ds18b20/mosquitto-2.0.9/cJSON.h"
#include"/home/xuhuinan1/ds18b20/mosquitto-2.0.9/platform.h"

#define INI_PATH  "/home/xuhuinan1/ds18b20/mosquitto-2.0.9/config.ini"
#if 0
typedef struct p_ctx_s {
        //      char                    *platform;
        char                    clientid[128];
        char                    hostname[128];
        char                    username[128];
        char                    password[128];
        char                    id[32];
        char                    js[128];
        int                     keepalive;
        int                     port;


}p_ctx_t;

typedef struct pub_ctx_s{
        char                    pub_topic[128];
        int                     pub_qos;



}pub_ctx_t;
typedef struct sub_ctx_s{
        char                    sub_topic[128];
        int                     sub_qos;

}sub_ctx_t;
typedef struct log_ctx_s{
        char    log_file[1024];
        int     log_level;
        int     log_size;
}log_ctx_t;

typedef struct info_ctx_s{
        char    T_time[128];
        char    name[128];
        float   temperature;
}info_ctx_t;

typedef struct msq_ctx_s{
        p_ctx_t         p_ctx;
        info_ctx_t      info_ctx;
        pub_ctx_t       pub_ctx;
        sub_ctx_t       sub_ctx;
        log_ctx_t       log_ctx;

}msq_ctx_t;
#endif

int  get_config(msq_ctx_t *msq){

        char                    section[128];
        dictionary              *ini=NULL;
        const   char            *str;

        //memset(msq,0,sizeof(*msq));
        /*加载ini文件，将数据存到ini结构中*/
        ini = iniparser_load(INI_PATH);
        if(!ini)
        {
                printf("no found config.ini\n");
                return -1;
        }
        printf("platform=%s\n",msq->p_ctx.platform);
        /*获取配置文件的value*/
        strncpy(msq->p_ctx.id,"MQTT_ds18b20_01",sizeof(msq->p_ctx.id));
        strncpy(msq->p_ctx.service_id,"1",sizeof(msq->p_ctx.service_id));
        strncpy(msq->p_ctx.type,"updata",sizeof(msq->p_ctx.type));
        msq->p_ctx.version=1;
        strncpy(msq->p_ctx.clientToken,"clientToken",sizeof(msq->p_ctx.clientToken));

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:clientid",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        printf("str=%s\n",str);
        strncpy(msq->p_ctx.clientid,str,strlen(str));
        printf("clientid = %s\n",msq->p_ctx.clientid);


        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:username",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        strncpy(msq->p_ctx.username, str,strlen(str));
        printf("username = %s\n", msq->p_ctx.username);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:hostname",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        //(data->hostname)=(char *)str;
        snprintf(msq->p_ctx.hostname,sizeof(msq->p_ctx.hostname),str);
        //strncpy(msq->p_ctx.hostname, str, strlen(str));
        printf("hostname = %s\n", msq->p_ctx.hostname);

#if 1
        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:password",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        strncpy(msq->p_ctx.password, str, strlen(str));
        printf("password = %s\n", msq->p_ctx.password);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:pub_topic",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        strncpy(msq->pub_ctx.pub_topic, str, strlen(str));
        printf("pub_topic = %s\n",msq->pub_ctx.pub_topic);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:sub_topic",msq->p_ctx.platform);
        str = iniparser_getstring(ini,section,"NULL");
        strncpy(msq->sub_ctx.sub_topic,str,strlen(str));
        printf("sub_topic = %s\n",msq->sub_ctx.sub_topic);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:port",msq->p_ctx.platform);
        //str = iniparser_getstring(ini,section,"NULL");
        //msq->pub_ctx.pub_topic=atoi(str);
        //strncpy(msq->pub_ctx.pub_topic, str, strlen(msq->pub_ctx.pub_topic));
        msq->p_ctx.port = iniparser_getint(ini, section,-1);
        printf("port = %d\n",msq->p_ctx.port);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:keepalive",msq->p_ctx.platform);
        msq->p_ctx.keepalive = iniparser_getint(ini, section,-1);
        printf("keepalive = %d\n",msq->p_ctx.keepalive);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:sub_qos",msq->p_ctx.platform);
        msq->sub_ctx.sub_qos = iniparser_getint(ini, section,-1);
        printf("sub_qos = %d\n",msq->sub_ctx.sub_qos);

        memset(section,0,sizeof(section));
        snprintf(section,32,"%s:pub_qos",msq->p_ctx.platform);
        msq->pub_ctx.pub_qos = iniparser_getint(ini, section,-1);
        printf("pub_qos = %d\n",msq->pub_ctx.pub_qos);




        //iniparser_freedict(ini);
#if 0
        if(strcmp("ali",platform)==0){

                //js="{\"params\": {\"CurrentTemperature\": %.2lf}}";
                snprintf(data.js,sizeof(data.js),"{\"params\": {\"CurrentTemperature\": \%.2lf}}");
                snprintf(data.name,sizeof(data.name), "0001");
                //name="0001";
                printf("js=%s\n",data.js);
                printf("name=%s\n",data.name);

        }

        else if(strcmp("huawei",platform)==0){
                snprintf(data.js,sizeof(data.js),"{\"services\":[{\"service_id\" : \"1\",\"properties\" : {\"CurrentTemperature\" :\%.2f}}]}");
                snprintf(data.name,sizeof(data.name),"0002");
                printf("js=%s\n",data.js);
                printf("name=%s\n",data.name);
        }
        else if(strcmp("tencent",platform)==0){
                snprintf(data.js,sizeof(data.js)," {\"type\": \"update\", \"state\": {\"reported\": {\"temperature\": %.2f}},\"version\": 1,\"clientToken\":\"clientToken\"}");
                snprintf(data.name,sizeof(data.name),"0003");
                printf("js=%s\n",data.js);
                printf("name=%s\n",data.name);
        }
        else{
                printf("check your platform input\n");
        }
#endif
        iniparser_freedict(ini);
        //return mosquitto;
        return 0;
        //return *data;
}
#endif
int pack_data(msq_ctx_t *msq,char *buf,int size){


        if(!msq->info_ctx.name||!msq->info_ctx.T_time||!msq->info_ctx.temperature){
                printf("get number failure\n");
                return -1;
        }
        snprintf(buf,size,"%s/%s/%f",msq->info_ctx.name,msq->info_ctx.T_time,msq->info_ctx.temperature);
        return 0;

}
#if 1
int get_json(msq_ctx_t *msq, char *buf)
{
        char             *msg;
        cJSON            *root;
        cJSON            *item,*item2;
        cJSON            *arr;
        char            *str1="ali";
        char            *str2="huawei";
        char            *str3="tencent";

        root =  cJSON_CreateObject();
        item =  cJSON_CreateObject();

        //memset(root, 0, sizeof(root));
        //memset(item, 0, sizeof(item));

        if(!strcmp(msq->p_ctx.platform,str1))
        {
                // cJSON_AddItemToObject(root, "method", cJSON_CreateString(st_iotd->st_mqtt.method));
                // cJSON_AddItemToObject(root, "id", cJSON_CreateString(st_iotd->st_mqtt.id));
                memset(root, 0, sizeof(root));
                memset(item, 0, sizeof(item));
                root = cJSON_CreateObject();
                cJSON_AddItemToObject(root, "params", item);
                cJSON_AddItemToObject(item, "CurrentTemperature", cJSON_CreateNumber(msq->info_ctx.temperature));
                //cJSON_AddItemToObject(root, "time", cJSON_CreateString(msq->info_ctx.T_time));
                // cJSON_AddItemToObject(root, "version", cJSON_CreateNumber(st_iotd->st_mqtt.version));


                // cJSON_AddItemToObject(root, "method", cJSON_CreateString(st_iotd->st_mqtt.method));
                // cJSON_AddItemToObject(root, "id", cJSON_CreateString(st_iotd->st_mqtt.id));
                // cJSON_AddItemToObject(root, "params", item);
                // cJSON_AddItemToObject(root, "time", cJSON_CreateString(st_iotd->st_collect.time));
                // cJSON_AddItemToObject(item, "CurrentTemperature", cJSON_CreateNumber(st_iotd->st_collect.temper));
                // cJSON_AddItemToObject(root, "version", cJSON_CreateNumber(st_iotd->st_mqtt.version));
        }

        if(!strcmp(msq->p_ctx.platform,str2))
        {
                memset(root, 0, sizeof(root));
                memset(item, 0, sizeof(item));
                memset(item2, 0, sizeof(item2));
                root = cJSON_CreateObject();
                cJSON_AddItemToObject(root, "services", arr = cJSON_CreateArray());
                cJSON_AddItemToArray(arr, item = cJSON_CreateObject());
                cJSON_AddItemToObject(item, "service_id", cJSON_CreateString(msq->p_ctx.service_id));
                cJSON_AddItemToObject(item, "properties", item2 = cJSON_CreateObject());
                cJSON_AddItemToObject(item2, "current_temperature", cJSON_CreateNumber(msq->info_ctx.temperature));
                //cJSON_AddItemToObject(root, "time", cJSON_CreateString(msq->info_ctx.T_time));
        }

        if(!strcmp(msq->p_ctx.platform,str3))
        {
                memset(root, 0, sizeof(root));
                memset(item, 0, sizeof(item));
                memset(item2, 0,sizeof(item2));
                root = cJSON_CreateObject();
                cJSON_AddItemToObject(root, "type", cJSON_CreateString(msq->p_ctx.type));
                cJSON_AddItemToObject(root, "state", item = cJSON_CreateObject());
                cJSON_AddItemToObject(item, "reported", item2 = cJSON_CreateObject());
                cJSON_AddItemToObject(item2, "temperature", cJSON_CreateNumber(msq->info_ctx.temperature));
                //cJSON_AddItemToObject(root, "time", cJSON_CreateString(msq->info_ctx.T_time));
                cJSON_AddItemToObject(root, "verson", cJSON_CreateNumber(msq->p_ctx.version));
                cJSON_AddItemToObject(root, "clientToken", cJSON_CreateString(msq->p_ctx.clientToken));
        }

        msg = cJSON_Print(root);
        strcpy(buf, msg);

        return 0;

}

#endif


#if 0
void get_json(void *sub_data){

        msq_ctx_t       *msq=(msq_ctx_t *)sub_data;
        cJSON           *json=NULL;
        cJSON           *item;
        char            *data;
        char            *str;
        int             len;

        if(NULL !=message->payloadlen){
                json=cJSON_Parse(message->payload);//获取整个大句柄
                if(!json){
                        printf("json parser failure:%s\n",strerror(errno));
                        return;
                }

                item=cJSON_GetObjectItemCaseSensitive(json,"id");//获取对象成员
                if(!item){
                        printf("get failure:%s",strerror(errno));
                        cJSON_Delete(item);
                        return;
                        str=cJSON_PrintUnformatted(item);
                        len=strlen(item);
                        memcpy(data,str,len);
                        if(strcmp(data,msq.p_ctx.id)){
                                cJSON_Delete(item);
                                cJSON_free(data);
                        }
                        cJSON_free(data);
                }

        }
}

#endif
